package gw.com.android.ui.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

import com.flyco.tablayout.SlidingTabLayout;

import www.com.library.util.DeviceUtil;

/**
 * @author jett
 * @since 2017-12-07.
 */
public class MyScrollView extends ScrollView {

    private boolean isTouching = false;
    private boolean wholeNewsMode = false;
    private View targetView;
    private OnScrollListener listener;

    public MyScrollView(Context context) {
        super(context);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (getChildCount() > 0) {
            ViewGroup container = (ViewGroup) getChildAt(0);
            int count = container.getChildCount();
            for (int i = 0; i < count; i++) {
                View child = container.getChildAt(i);
                if (child instanceof SlidingTabLayout) {
                    targetView = child;
                }
            }
        }
    }

    public void setWholeNewsMode(boolean wholeNewsMode) {
        this.wholeNewsMode = wholeNewsMode;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isTouching = true;
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                if (!wholeNewsMode) {
                    int suckHeight = getSuckHeight();
                    if (suckHeight > 0 && getScrollY() > suckHeight) {
                        smoothScrollTo(0, 99999);
                        return true;
                    }
                }
                isTouching = false;
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (!wholeNewsMode && !isTouching) {
            int suckHeight = getSuckHeight();
            if (suckHeight > 0 && t > suckHeight) {
                wholeNewsMode = true;
                smoothScrollTo(0, 99999);
            }
        }
        if (listener != null) {
            listener.onScroll(t, t - oldt);
        }
    }

    private int getSuckHeight() {
        if (targetView != null) {
            return targetView.getTop() - DeviceUtil.instance().dip2px(120, getContext());
        }
        return 0;
    }

    public void setScrollListener(OnScrollListener listener) {
        this.listener = listener;
    }

    public interface OnScrollListener {
        void onScroll(int top, int distance);
    }

}
